<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_buhuo/model')->方法名();
*/
namespace jy_buhuo;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function addorder($order=array(),$plugin_name=''){
			$buhuoset=$this->plugin_setting('basic');
			if(!$buhuoset['isopen']){
				return false;
			}
			$member=pdo_get('jy_commission_member',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),array('mid','is_agent','level_id','agent_id','createtime'));
			if(!$member['is_agent']){
				return false;
			}
			$new_data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$order['mid'],
					'ordersn'=>trim($order['ordersn']),
					'price'=>trim($order['price']),
					'plugin_name'=>$plugin_name,
					'createtime'=>TIMESTAMP,
			);
			$this->insert('order',$new_data);
			if($member['agent_id']){
				foreach($order['goods'] as $goods){
					$lasttotal = pdo_getcolumn('jy_buhuo_goods',array('goodsid'=>$goods['goodsid'],'mid'=>$member['agent_id'],'plugin_name'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']),'total');
					$lasttotal=$lasttotal?:0;
					if($lasttotal>$total){
						pdo_update('jy_buhuo_goods',array('total -='=>$total),array('goodsid'=>$goods['goodsid'],'mid'=>$member['agent_id'],'plugin_name'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']));
						return true;
					}else{
						return false;
					}
				}
			}
			$this->addgoods($order,$plugin_name);
			return true;
	}
	public function addgoods($order=array(),$plugin_name=''){
		if($plugin_name=='jy_weishop'){
			$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$order['id'],'uniacid'=>$_SESSION['uniacid']),array('goodsid','optionid','price','total','goodssn','productsn'));
			foreach($ordergoods as $gv){
				$gv['total']=$gv['total']?:1;
				$isgoods = pdo_get('jy_buhuo_goods',array('goodsid'=>$gv['goodsid'],'mid'=>$order['mid'],'plugin_name'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']),array('id'));
				if($isgoods['id']){
					 pdo_update('jy_buhuo_goods',array('alltotal +='=>$gv['total'],'total +='=>$gv['total']),array('goodsid'=>$gv['goodsid'],'mid'=>$order['mid'],'plugin_name'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']));
				}else{
					$new_data=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$order['mid'],
							'goodsid'=>$gv['goodsid'],
							'total'=>$gv['total'],
							'alltotal'=>$gv['total'],
							'plugin_name'=>$plugin_name,
							'createtime'=>TIMESTAMP,
					);
					$this->insert('goods',$new_data);
				}
			}
		}elseif($plugin_name=='jy_onlineedu'){
				$total=1;
				$isgoods = pdo_get('jy_buhuo_goods',array('goodsid'=>$gv['goodsid'],'mid'=>$order['mid'],'plugin_name'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']),array('id'));
				if($isgoods['id']){
					 pdo_update('jy_buhuo_goods',array('alltotal +='=>$total,'total +='=>$total),array('goodsid'=>$gv['goodsid'],'mid'=>$order['mid'],'plugin_name'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']));
				}else{
					$new_data=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$order['mid'],
							'goodsid'=>$order['video_id'],
							'total'=>$total,
							'alltotal'=>$total,
							'plugin_name'=>$plugin_name,
							'createtime'=>TIMESTAMP,
					);
					$this->insert('goods',$new_data);
				}
		}
		return true;
	}
	//更新库存
	public function uptotal($agent_id=0,$goodsid=0,$total=1,$plugin_name=''){
			if(!$agent_id){
				return true;
			}
			$buhuoset=$this->plugin_setting('basic');
			if(!$buhuoset['isopen']){
				return true;
			}
			$lasttotal = pdo_getcolumn('jy_buhuo_goods',array('goodsid'=>$goodsid,'mid'=>$agent_id,'plugin_name'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']),'total');
			$lasttotal=$lasttotal?:0;
			if($lasttotal>$total){
				pdo_update('jy_buhuo_goods',array('total -='=>$total),array('goodsid'=>$goodsid,'mid'=>$agent_id,'plugin_name'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']));
				return true;
			}else{
				return false;
			}
	}
	public function butotal($mid=0,$goodsid=0,$total=1,$plugin_name=''){
			$isgoods = pdo_get('jy_buhuo_goods',array('goodsid'=>$goodsid,'mid'=>$mid,'plugin_name'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']),array('id'));
			if($isgoods['id']){
				pdo_update('jy_buhuo_goods',array('alltotal +='=>$total,'total +='=>$total),array('goodsid'=>$goodsid,'mid'=>$mid,'plugin_name'=>$plugin_name,'uniacid'=>$_SESSION['uniacid']));
			}else{
				$new_data=array(
							'uniacid'=>$_SESSION['uniacid'],
							'mid'=>$mid,
							'goodsid'=>$goodsid,
							'total'=>$total,
							'alltotal'=>$total,
							'plugin_name'=>$plugin_name,
							'createtime'=>TIMESTAMP,
				);
				$this->insert('goods',$new_data);
			}
			return true;
	}
}
?>